<template>
  <div class="home">
    <el-form ref="form" :model="dishes" label-width="80px">
      <el-form-item label="菜品名称">
        <el-input v-model="dishes.dishName"></el-input>
      </el-form-item>
      <el-form-item label="菜品价格">
        <el-input
          v-model="dishes.price"
          onkeyup="this.value = this.value.replace(/[^\d.]/g,'');"
          placeholder="请输入菜品价格"
        ></el-input>
      </el-form-item>
      <el-form-item label="菜品分类">
        <el-select v-model="dishes.category.id" placeholder="请选择菜品分类">
          <!-- label是菜品分类名称  value是要传过去的菜品分类id  v-for 页面动态创建菜品分类 需要循环遍历数组  -->
          <el-option
            v-for="category in categoryList"
            :key="category.id"
            :label="category.dishCategoryName"
            :value="category.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <!-- action必选参数  图片上传的地址 -->
      <el-form-item label="菜品图片">
        <el-upload
          :on-success="uploaderSuccess"
          :file-list="fileList"
          action=" http://localhost:8080/uploader/dishImage"
          list-type="picture-card"
        >
          <i slot="default" class="el-icon-plus"></i>
          <div slot="file" slot-scope="{file}">
            <!-- 绑定的图片 -->
            <img class="el-upload-list__item-thumbnail" :src="dishes.picPath" alt />
            <span class="el-upload-list__item-actions">
              <!-- 放大镜 -->
              <span class="el-upload-list__item-preview" @click="handlePictureCardPreview(file)">
                <i class="el-icon-zoom-in"></i>
              </span>

              <!-- 删除按钮 -->
              <span
                v-if="!disabled"
                class="el-upload-list__item-delete"
                @click="handleRemove(file)"
              >
                <i class="el-icon-delete"></i>
              </span>
            </span>
          </div>
        </el-upload>
        <!-- 放大镜的弹窗 -->
        <el-dialog :visible.sync="dialogVisible">
          <img width="100%" :src="dialogImageUrl" alt />
        </el-dialog>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="saveDish">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  data() {
    return {
      dialogImageUrl: "",
      dialogVisible: false,
      disabled: false,
      categoryList: [],
      fileList: [],
      dishes: {
        id: "",
        dishName: "",
        price: "",
        picPath: "",
        category: {
          id: ""
        }
      }
    };
  },
  created() {
    // 判断url地址上面有没有参数
    this.init();
  },
  watch: {
    // 路由发生改变的时候会触发
    $route(to, from) {
      this.init();
    }
  },

  methods: {
    init() {
      // 判断url地址上面有没有参数
      if (this.$route.params && this.$route.params.id) {
        this.dishes.id = this.$route.params.id;
        this.$ajax
          .post("http://localhost:8080/dish/findOne/" + this.$route.params.id)
          // 得到结果集
          .then(res => {
            // 绑定到页面上
            this.dishes = res.data.date.dish;
            this.fileList = [{ name: "测试", url: this.dishes.picPath }];
          })
          .catch(err => {
            console.log(err);
            this.$message.error("连接超时请检查网络");
          });
      } else {
        this.fileList = [];
        this.dishes = {
          id: "",
          dishName: "",
          price: "",
          picPath: "",
          category: {
            id: null
          }
        };
      }
      this.findDishCategoryAll();
    },
    findDishCategoryAll() {
      // 发起请求
      this.$ajax
        .post("http://localhost:8080/dishCategory/all")
        // 得到结果集
        .then(res => {
          // 绑定到页面上
          this.categoryList = res.data.date.list;
        })
        .catch(err => {
          console.log(err);
          this.$message.error("连接超时请检查网络");
        });
    },
    // 上传成功之后图片回显 在这里绑定数据就可以回显
    uploaderSuccess(res, file) {
      console.log(res);
      console.log(file);
      this.dishes.picPath = res.date.imgPath;
    },
    // 放大镜
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    // 删除按钮
    handleRemove(file) {
      // 修改菜品
      this.dishes.picPath == null;
      this.fileList = [];
    },
    saveDish() {
      //没有id 添加菜品
      if (this.dishes.id == "") {
        this.$ajax
          .post("http://localhost:8080/dish/addDish", this.dishes)
          // 得到结果集
          .then(res => {
            if (res.data.code == 200) {
              this.$message({
                type: "success",
                message: "添加菜品成功!"
              });
              // 添加失败走这里
            } else {
              this.$message.error(res.data.message);
            }
            // 跳转页面
            this.$router.push("/showDish");
          })

          .catch(err => {
            console.log(err);
            this.$message.error("连接超时请检查网络");
          });
      } else {
        // 有id修改菜品
        this.$ajax
          .post("http://localhost:8080/dish/update", this.dishes)
          .then(res => {
            if (res.data.code == 200) {
              this.$message({
                message: "修改菜品成功",
                type: "success"
              });
            } else {
              this.$message.error(res.data.message);
            }
            this.$router.push("/showDish");
          })
          .catch(err => {
            console.log(err);
            this.$message.error("连接超时请检查网络");
          });
      }
    }
  }
};
</script>
<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.avatar-uploader >>> .el-upload {
  border: 1px dashed #dcdfe6;
}
</style>